package com.fy.common.spring;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Component
public class LocaleMessageSource {
    private static MessageSource messageSource;

    public LocaleMessageSource(MessageSource messageSource) {
        LocaleMessageSource.messageSource = messageSource;
    }

    /**
     * 获取message
     * @param code
     * @return
     */
    public String getMessage(String code) {
        return getMessage(code, null);
    }

    /**
     * 获取message
     * @param code
     * @param args
     * @return
     */
    public String getMessage(String code, Object[] args) {
        return getMessage(code, args, "");
    }

    public String getMessage(String code, Object[] args, String defaultMessage) {
        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage(code, args, defaultMessage, locale);
    }
}
